/*
 * Copyright (C) 2010-2010 MaoJian Wu
 *
 * Author:
 *	MaoJian Wu
 *
 * Source:
 *	
 *
 * This file is a part of zfspear library.
 *
 *
 */

#include "roster_item.h"
#include "group.h"

RosterItem::RosterItem() :
    XmppElement("item") {
}
Jid RosterItem::GetJid() {
   Jid jid;
   jid.FromString(GetTag("jid"));
   return jid;
}

void RosterItem::SetJid(Jid jid) {
   SetTag("jid",jid.ToString());
}

QString RosterItem::Name() {
   return GetTag("jid");
}

void RosterItem::SetName(const QString& name) {
   SetTag("name",name);
}

QString RosterItem::Subscription() {
   return GetTag("subscription");
}

void RosterItem::SetSubscription(const QString& subscription) {
  SetTag("subscription",subscription);
}
QList<XmppElement*> RosterItem::GetGroups() {
   return this->SelectElements("group",false);
}

void RosterItem::AddGroup(const QString& groupname) {
    Group *g = new Group(groupname);
    this->AppendChild(g);
}

bool RosterItem::HasGroup(const QString& groupname) {
    QList<XmppElement*> groups = GetGroups();
    QListIterator<XmppElement*> iterator(groups);
    while (iterator.hasNext()) {
         if (iterator.next()->TagName() == groupname)
              return true;
    }
    return false;
}
void RosterItem::RemoveGroup(const QString& groupname) {
    QList<XmppElement*> groups = GetGroups();
    QListIterator<XmppElement*> iterator(groups);
    while (iterator.hasNext()) {
         XmppElement* e=iterator.next();
         if ( e->TagName()== groupname) {
            this->RemoveChild(e);
            return;
         }
    }

}
